package com.dcbeida.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.R;
import com.dcbeida.bean.SalsesDetail;
import com.dcbeida.bean.ZyDrugDetail;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 销售详情库存
 */
public class ZyDetail2Adapter extends BaseAdapter {

    private ArrayList<ZyDrugDetail.ListDTO.DrugListDTO> mArrayList = new ArrayList<>();
    private Context mContext;

    public ZyDetail2Adapter(Context context) {
        super();
        this.mContext = context;
    }


    @Override
    public int getCount() {
        if (mArrayList == null) {
            return 0;
        } else {
            return this.mArrayList.size();
        }
    }

    @Override
    public Object getItem(int position) {
        if (mArrayList == null) {
            return null;
        } else {
            return this.mArrayList.get(position);
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("NewApi")
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {

            convertView = LayoutInflater.from(mContext).inflate(R.layout.with_thxq_item, null, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        ZyDrugDetail.ListDTO.DrugListDTO bean = mArrayList.get(position);


        if (bean.isFirst())
            holder.lin_item_head.setVisibility(View.VISIBLE);
        else
            holder.lin_item_head.setVisibility(View.GONE);

        holder.tv_yp_name.setText(bean.getDrugname());
        holder.tv_count.setText(bean.getAllQuantity() + "");


        holder.tvPh.setText(mContext.getString(R.string.ph) + bean.getBatchnumber());
        holder.tvBh.setText(bean.getDrugcode());
        holder.tvTime.setText(mContext.getString(R.string.yxq) + bean.getExpiredate());
        holder.tvBox.setText(bean.getQuantity() + "");

        //        status:1,	//订单1表示有效，当为退货单时，0表示未审，1表示已经审核，取消的退货单不会出现在列表中。
        holder.ll_bg_no.setVisibility(View.GONE);
        holder.tvStatus.setVisibility(View.GONE);
        if (bean.getRefundid().equals("0")) {
            //正常
            holder.ivStatus.setVisibility(View.INVISIBLE);
            holder.llBg.setBackground(mContext.getResources().getDrawable(R.drawable.shape_white_r4));
        } else {
            if (bean.getAuditstatus().equals("0")) {
                // 0未审核，1已经审核
                //退款成功
                holder.tvStatus.setVisibility(View.VISIBLE);
                holder.tvStatus.setText(R.string.shz);
                holder.tvStatus.setTextColor(mContext.getResources().getColor(R.color.c_1AAD19));
                holder.ivStatus.setVisibility(View.VISIBLE);
                holder.llBg.setBackground(mContext.getResources().getDrawable(R.drawable.shape_white_r4));
            } else {
                //退款成功
                holder.ivStatus.setVisibility(View.VISIBLE);
                holder.llBg.setBackground(mContext.getResources().getDrawable(R.drawable.shape_f4_r4));
            }


        }

        return convertView;
    }


    public ArrayList<ZyDrugDetail.ListDTO.DrugListDTO> getmArrayList() {
        return mArrayList;
    }

    public void setmArrayList(List<ZyDrugDetail.ListDTO.DrugListDTO> mArrayList) {
        if (mArrayList.size() >= 0) {
            this.mArrayList.addAll(mArrayList);
        }
        notifyDataSetChanged();
    }

    public void clearList() {
        this.mArrayList.clear();
    }


    static class ViewHolder {

        @Bind(R.id.lin_item_head)
        LinearLayout lin_item_head;
        @Bind(R.id.tv_yp_name)
        TextView tv_yp_name;
        @Bind(R.id.tv_count)
        TextView tv_count;
        @Bind(R.id.tv_headName)
        TextView tv_headName;

        @Bind(R.id.tv_ph)
        TextView tvPh;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.tv_bh)
        TextView tvBh;
        @Bind(R.id.tv_box)
        TextView tvBox;
        @Bind(R.id.tv_status)
        TextView tvStatus;
        @Bind(R.id.iv_status)
        ImageView ivStatus;
        @Bind(R.id.ll_bg)
        RelativeLayout llBg;
        @Bind(R.id.ll_bg_no)
        RelativeLayout ll_bg_no;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            tv_headName.setVisibility(View.GONE);
        }
    }
}
